package mn.idax.exchange.otc.util;

import com.alibaba.fastjson.JSON;
import mn.idax.exchange.common.SpringUtils;
import mn.idax.exchange.common.bean.ResponseData;
import mn.idax.exchange.otc.service.impl.ChatLogServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Locale;

/**
 * @Auther: dison
 * @Date: 2018/9/29 09:50
 * @Description:
 */
public class I18nUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(I18nUtil.class);
    public static String getMessage(String code, String lang, Object... dynamicParam) {
        String messageCode = "message." + code;
        Locale locale = null;
        if (lang == null) {
            lang = "zh_cn";
        }

        String langArr[] = lang.replace("-", "_").split("_");

        if (langArr != null && langArr.length > 1) {
            locale = new Locale(langArr[0], langArr[1]);
        } else {
            locale = getDefaultCountry(langArr[0]);
        }

        String messageBody = SpringUtils.getMessage(messageCode, locale, dynamicParam);
        LOGGER.info("getMessage lang {},locale {},messageBody {}",lang,locale,messageBody);

        return messageBody;
    }

    public static String getMessage(String code, Locale locale, Object... dynamicParam) {
        String messageCode = "message." + code;
        String messageBody = SpringUtils.getMessage(messageCode, locale, dynamicParam);
        return messageBody;
    }

    public static Locale getDefaultCountry(String language) {
        if (language.equalsIgnoreCase("zh")) {
            return new Locale(language, "cn");
        }
        return new Locale(language, "");
    }


    public static Object replaceLangeage(String returnJson, String... obj) {
        if (returnJson == null) {
            return returnJson;
        } else {
            ResponseData responseData = null;
            String message = null;

            int code;
            try {
                responseData = JSON.parseObject(returnJson, ResponseData.class);
                if (responseData == null) {
                    return returnJson;
                }

                code = responseData.getSuccess();
                String messageCode = "message." + String.valueOf(code);
                message = SpringUtils.getMessage(messageCode, obj);
            } catch (Exception var5) {
                return returnJson;
            }

            return ResponseData.returnResponse(code, message, responseData.getData());
        }
    }
}
